﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;

namespace Test.Console
{
    class Downloading
    {
        static WebClient wc = new WebClient();

        static void DownloadFile()
        {
            wc.DownloadFileCompleted += new System.ComponentModel.AsyncCompletedEventHandler(wc_DownloadFileCompleted);
            wc.DownloadProgressChanged += new DownloadProgressChangedEventHandler(wc_DownloadProgressChanged);
            wc.DownloadFileAsync(new Uri("http://localhost:7070/Test.Website/ToDownload.txt", true), "C:\\Target.txt");
            //wc.DownloadFile(new Uri("http://localhost:7070/Test.Website/ToDownload.txt", true), "C:\\Target.txt");

            System.Console.Read();

            //WebRequest req = WebRequest.Create("http://localhost:7070/ToDownload.txt");
            //.BeginGetRequestStream(new AsyncCallback(AsyncDownload), state);
        }

        static void wc_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            System.Console.WriteLine(e.ProgressPercentage + "% complete");
        }

        static void wc_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            if (e.Error == null)
                System.Console.WriteLine("Complete");
            else
            {
                System.Console.WriteLine(e.Error.Message);
                System.Console.WriteLine(e.Error.StackTrace);
            }
        }
    }
}